package com.lzjn.code.web.dao;

import com.lzjn.code.common.base.CommonDao;
import com.lzjn.code.common.model.PageModel;
import com.lzjn.code.web.entity.TableConfig;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Repository;

import java.sql.Connection;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@Repository
public class TableConfigDao extends CommonDao<TableConfig> {

	public PageModel getByPage(TableConfig tableConfig, PageModel page){
		
		StringBuilder sql = new StringBuilder();
		sql.append("select id,table_name,entity_name,description,is_checkbox,is_page,")
		   .append("is_tree,search_column_num,form_column_num ")
		   .append("from table_config where 1=1");
		
		List<Object> params = new ArrayList<>();

		if(StringUtils.isNotEmpty(tableConfig.getTableName())) {
			
			sql.append(" and table_name like ?");
			params.add("%"+tableConfig.getTableName()+"%");
		}
		if(StringUtils.isNotEmpty(tableConfig.getDescription())) {
			
			sql.append(" and description like ?");
			params.add("%"+tableConfig.getDescription()+"%");
		}
		sql.append(" order by create_time desc");
		
		page = queryByPage(sql.toString(), params, page);
		
		return page;
	}

	public TableConfig getById(String id){
		
		StringBuilder sql = new StringBuilder();
		sql.append("select id,table_name,entity_name,description,is_checkbox,is_page,")
		   .append("is_tree,search_column_num,form_column_num ")
		   .append("from table_config where id = ?");
		
		return queryById(sql.toString(), id);
	}
	
	public List<TableConfig> getByIds(String ids){
		
		ids = ids.replace(",", "','");
		
		StringBuilder sql = new StringBuilder();
		sql.append("select id,table_name,entity_name,description,is_checkbox,is_page,")
		   .append("is_tree,search_column_num,form_column_num ")
		   .append("from table_config where id in ('").append(ids).append("')");
		
		return query(sql.toString(), null);
	}
	
	public void add(TableConfig tableConfig, Connection conn) throws Exception{
		
		StringBuilder sql = new StringBuilder();
		sql.append("insert into table_config(id,table_name,entity_name,description,")
		   .append("is_checkbox,is_page,is_tree,search_column_num,form_column_num,create_time) ")
		   .append("values(?,?,?,?,?,?,?,?,?,?)");
		
		List<Object> params = new ArrayList<>();
		params.add(tableConfig.getId());
		params.add(tableConfig.getTableName());
		params.add(tableConfig.getEntityName());
		params.add(tableConfig.getDescription());
		params.add(tableConfig.getIsCheckbox());
		params.add(tableConfig.getIsPage());
		params.add(tableConfig.getIsTree());
		params.add(tableConfig.getSearchColumnNum());
		params.add(tableConfig.getFormColumnNum());
		params.add(new Date());

		executeUpdate(sql.toString(), params, conn);
	}
	
	
	public void update(TableConfig tableConfig, Connection conn) throws Exception{
		
		StringBuilder sql = new StringBuilder();
		sql.append("update table_config set ")
		   .append("table_name = ?,entity_name = ?,description = ?,is_checkbox = ?,is_page = ?,")
		   .append("is_tree = ?,search_column_num = ?,form_column_num = ? ")
		   .append("where id = ?");
		
		List<Object> params = new ArrayList<>();
		
		params.add(tableConfig.getTableName());
		params.add(tableConfig.getEntityName());
		params.add(tableConfig.getDescription());
		params.add(tableConfig.getIsCheckbox());
		params.add(tableConfig.getIsPage());
		params.add(tableConfig.getIsTree());
		params.add(tableConfig.getSearchColumnNum());
		params.add(tableConfig.getFormColumnNum());
		params.add(tableConfig.getId());
		
		executeUpdate(sql.toString(), params);
	}
	
	public void delete(String id, Connection conn) throws Exception{
		
		String sql = "delete from table_config where id = ?";
		
		List<Object> params = new ArrayList<>();
		params.add(id);
		
		executeUpdate(sql, params, conn);
	}
	
	public void batchDelete(String ids, Connection conn) throws Exception{
		
		ids = ids.replace(",", "','");
		
		String sql = "delete from table_config where id in ('" + ids + "')";
		
		executeUpdate(sql, null, conn);
	}
}
